<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#--引入外部文件-->
    <#include "../common/header.ftl">
</head>
<body>

<div class="container" style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-3">
            <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">攻略文章管理</h1>
                </div>
            </div>
            <#--高级查询--->
            <form class="form-inline" id="searchForm" action="/strategyDetail/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
            </form>
            <a href="javascript:" class="btn btn-success inputBtn">
                <span class="glyphicon glyphicon-plus"></span> 添加
            </a>

            <table class="table table-striped table-hover" >
                <tr>
                    <th>编号</th>
                    <th>封面</th>
                    <th>标题</th>
                    <th>发布时间</th>
                    <th>排序</th>
                    <th>所属类别</th>
                    <th>状态</th>
                </tr>
                <#--使用Freemark的指令完成循环迭代-->
                <#list pageInfo.list as entity>
                    <tr>
                        <td>${entity_index + 1}</td>
                        <td><img src="${entity.coverUrl}" width="30px" /></td>
                        <td>${entity.title}</td>
                        <td>${(entity.releaseTime?string('yyyy-MM-dd HH:mm:ss'))!}</td>
                        <td>${entity.sequence}</td>
                        <td>${entity.catalog.name}</td>
                        <td>
                                <#if entity.state== 0 >
                                   草稿
                                    <#elseif entity.state==1>
                                   发布
                                </#if>
                        </td>
                        <td>
                            <a href="javascript:" class="btn btn-info btn-xs inputBtn" data-json='${entity.json}'>
                                <span class="glyphicon glyphicon-pencil"></span> 修改
                            </a>
                        </td>
                    </tr>
                </#list>
            </table>

            <#include "../common/page.ftl">
        </div>
    </div>
</div>


<#--模态框必须是body的子元素-->
<div class="modal fade" id="inputModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog ">
        <div class="modal-content">
        <#--模态框标题-->
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">新增/编辑</h4>
            </div>
        <#--模态框正文-->
            <div class="modal-body">
            <#--填充编辑界面-->
                <form class="form-horizontal" action="/strategyDetail/saveOrUpdate.do" method="post" id="editForm" enctype="multipart/form-data">
                    <input type="hidden" name="id"/>
                    <div class="form-group" >
                        <label class="col-lg-4 control-label">标题：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="title" placeholder="请输入标题"/>
                        </div>
                    </div>
                    <div class="form-group" >
                        <label class="col-lg-4 control-label">封面：</label>
                        <div class="col-lg-6">
                            <img  id="coverImg" width="100%"> <!--用来显示-->
                            <input type="hidden" class="form-control" name="coverUrl" /><!--用来提交表单(回显)-->
                            <input type="file" class="form-control" name="file" /><!--用来提交表单(上传)-->
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">所属攻略：</label>
                        <div class="col-lg-6">
                            <select class="form-control" id="strategySelect">
                                <#list strategies as s>
                                    <option value="${s.id}">${s.title}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">攻略类别：</label>
                        <div class="col-lg-6">
                            <select class="form-control"  id="catalogSelect" name="catalog.id">
                            </select>
                        </div>
                    </div>
                    <div class="form-group" >
                        <label class="col-lg-4 control-label">序号：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="sequence" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">状态：</label>
                        <div class="col-lg-6">
                            <select name="state" class="form-control">
                                <option value="0">草稿</option>
                                <option value="1">发布</option>
                            </select>
                        </div>
                    </div>

                     <textarea name="strategyContent.content" id="editor1" ></textarea>
                </form>

            </div>
            <#--模态框底部按钮-->
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary submitDetailBtn">保存</button>
            </div>
        </div>
    </div>
</div>

<!--ckeditor-->
<script src="/js/plugins/ckeditor/ckeditor.js"></script>

<script>
    $(function () {
        //初始化ckeditor
        var ck = CKEDITOR.replace( 'editor1' );

        //模态框的表单提交
        $(".submitDetailBtn").click(function() {
            //获取编辑器中的内容,设置到textarea里面
            var data = ck.getData();
            $("#editor1").html(data);
            //提交表单
            $("#editForm").submit();
        });


        $(".inputBtn").click(function () {
            //1:先清空模态框的数据,再来做操作
            $("#inputModal input").val("");
            //2:获取当前按钮中的自定义JSON对象
            var obj = $(this).data("json");
            console.log(obj);
            //3:只有编辑按钮才做数据回显
            if (obj) {
                $("#inputModal input[name='id']").val(obj.id);
                $("#inputModal input[name='title']").val(obj.title);
                $("#inputModal select[name='state']").val(obj.state);
                $("#inputModal input[name='coverUrl']").val(obj.coverUrl);
                $("#inputModal input[name='sequence']").val(obj.sequence);
                $("#coverImg").attr('src',obj.coverUrl);

                //回显攻略下拉框
                $("#strategySelect").val(obj.strategyId);

                //根据攻略id获取所有的攻略分类
                $.get('/strategyCatalog/listByStrategyId.do',{strategyId:obj.strategyId},function (data) {
                    var temp  = "";
                    $.each(data,function (index, ele) {
                        temp += '<option value="'+ele.id+'">'+ele.name+'</option>';
                    })
                    $("#catalogSelect").html(temp);
                    //回显分类下拉框
                    $("#catalogSelect").val(obj.catalogId);
                })

                //回显攻略文章内容
                $.get('/strategyDetail/getContentById.do',{id:obj.id},function(data){
                    ck.setData(data.content);
                })


            }
            $("#inputModal").modal('show');
        })
        
        //攻略下拉框
        $("#strategySelect").change(function () {
            //获取当前选择的攻略的id
            var strategyId = $(this).val();
            //根据攻略id获取所有的攻略分类
            $.get('/strategyCatalog/listByStrategyId.do',{strategyId:strategyId},function (data) {
                var temp  = "";
                $.each(data,function (index, ele) {
                    temp += '<option value="'+ele.id+'">'+ele.name+'</option>';
                })
                $("#catalogSelect").html(temp);
            })

        })
        




        
    })
</script>

</body>
</html>